<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of admin
 *
 * @author QUYET
 */
class Admin extends Admin_Controller {

    //put your code here
    public function __construct() {
        parent::__construct();
        $this->load->model('Product_m');
        $this->load->library('categories_lib');
        $this->load->library('gidaff_upload');
    }

    function index() {
        $this->listing();
    }

    function listing() {
        $this->data['module_title'] = 'Quản lý sản phẩm';
        $this->template
                ->set_hook('TOOLBAR', 'product_toolbar')
                ->build('admin/listing', $this->data);
    }

    function json_product_list() {
        $data = $this->Product_m->get_all_products();
        echo '{identifier: "id",label: "name",items:' . json_encode($data) . '}';
    }

    function json_product_categories() {
        $this->categories_lib->build_tree_option(2, 'Danh mục sản phẩm', 1);
        echo '{identifier:"id",label:"name",items:' . json_encode($this->categories_lib->option_store) . '}';
    }

    function add() {
        $this->data['module_title'] = 'Thêm sản phẩm';
        $this->template
                ->set_hook('TOOLBAR', 'product_toolbar')
                ->build('admin/add', $this->data);
    }

    function add_product() {
        $config['upload_path'] = "./assets/img/p/";
        $config['download_path'] = base_url()."assets/img/p/";
        $this->gidaff_upload->initialize($config);
        if($this->gidaff_upload->do_upload()){
            $upload_data = $this->gidaff_upload->upload_data();
            //Tu day co the process csdl
            print_r($upload_data);echo 'Co upload du lieu';die;
        }
    }

    function upload_images() {
        $upload_path = "./assets/img/p/";  // where image will be uploaded, relative to this file
        $download_path = base_url()."assets/img/p/"; // same folder as above, but relative to the HTML file
        $postdata = array();
        $htmldata = array();
        $data = "";
        foreach ($_POST as $nm => $val) {
            $data .= $nm . "=" . $val . ","; // string for flash
            $postdata[$nm] = $val;   // array for html
        }
        $fieldName = "flashUploadFiles"; //Filedata";

        if (isset($_FILES[$fieldName]) || isset($_FILES['uploadedfileFlash'])) {
            $returnFlashdata = true; //for dev

            if (isset($_FILES[$fieldName])) {
                $m = move_uploaded_file($_FILES[$fieldName]['tmp_name'], $upload_path . $_FILES[$fieldName]['name']);
                $name = $_FILES[$fieldName]['name'];
            } else {
                $m = move_uploaded_file($_FILES['uploadedfileFlash']['tmp_name'], $upload_path . $_FILES['uploadedfileFlash']['name']);
                $name = $_FILES['uploadedfileFlash']['name'];
            }

            $file = $upload_path . $name;
            try {
                list($width, $height) = getimagesize($file);
            } catch (Exception $e) {
                $width = 0;
                $height = 0;
            }
            $type = $this->getImageType($file);
            $data .='file=' . $file . ',name=' . $name . ',width=' . $width . ',height=' . $height . ',type=' . $type;
            if ($returnFlashdata) {
                echo($data);
                return;
            }
        } elseif (isset($_FILES['uploadedfiles'])) {
            $cnt = 0;
            $_post = $postdata;
            $htmldata = array();
            $len = count($_FILES['uploadedfiles']['name']);
            for ($i = 0; $i < $len; $i++) {
                $moved = move_uploaded_file($_FILES['uploadedfiles']['tmp_name'][$i], $upload_path . $_FILES['uploadedfiles']['name'][$i]);
                if ($moved) {
                    $name = $_FILES['uploadedfiles']['name'][$i];
                    $file = $upload_path . $name;
                    $type = $this->getImageType($file);
                    try {
                        list($width, $height) = getimagesize($file);
                    } catch (Exception $e) {
                        $width = 0;
                        $height = 0;
                        $_post['filesInError'] = $name;
                    }

                    if (!$width) {
                        $_post['filesInError'] = $name;
                        $width = 0;
                        $height = 0;
                    }

                    $_post['file'] = $download_path . $name;
                    $_post['name'] = $name;
                    $_post['width'] = $width;
                    $_post['height'] = $height;
                    $_post['type'] = $type;
                    $_post['size'] = filesize($file);
                    $htmldata[$cnt] = $_post;
                } elseif (strlen($_FILES['uploadedfiles']['name'][$i])) {
                    $htmldata[$cnt] = array("ERROR" => "File could not be moved: (for loop) " . $_FILES['uploadedfiles']['name'][$i]);
                }
                $cnt++;
            }

            $data = json_encode($htmldata);
            print $data;
            return $data;
        } elseif (isset($_FILES['uploadedfile'])) {
            $m = move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $upload_path . $_FILES['uploadedfile']['name']);
            $name = $_FILES['uploadedfile']['name'];
            $file = $upload_path . $name;
            $type = $this->getImageType($file);
            try {
                list($width, $height) = getimagesize($file);
            } catch (Exception $e) {
                $width = 0;
                $height = 0;
            }
            $htmldata['file'] = $download_path . $name;
            $htmldata['name'] = $name;
            $htmldata['width'] = $width;
            $htmldata['height'] = $height;
            $htmldata['type'] = $type;
            $htmldata['size'] = filesize($file);
            $htmldata['additionalParams'] = $postdata;
        } elseif (isset($_GET['rmFiles'])) {
            $rmFiles = explode(";", $_GET['rmFiles']);
        } else {
            $htmldata = array("ERROR" => "Improper data sent - no files found");
        }
        $data = json_encode($htmldata);
        print_r($_POST);echo '<br/>';
        //print_r($htmldata);
    }

    function getImageType($filename) {
        return strtolower(substr(strrchr($filename, "."), 1));
    }

}
